package com.treasure.web.service;

import com.treasure.common.enums.IdentitiesEnum;
import com.treasure.common.model.SessionUser;
import com.treasure.common.reponse.Response;
import com.treasure.common.utils.SnowFlakeId;
import com.treasure.infrastructure.model.po.CorpUserInfo;
import com.treasure.web.cache.SessionUserCache;
import com.treasure.web.interceptor.AuthInterceptor;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Objects;

/**
 * @author sunhongmin
 * @date 2023/9/11 21:43
 * @description 登录管理
 */
public class LoginHelper {
    protected static final String SESSION_TOKEN_KEY = "SESSION_TOKEN_";
    protected static final String TOKEN_HEADER_KEY = "token";
    public static final String TOKEN_SPLIT = "#";

    @NotNull
    protected static String getToken(CorpUserInfo sessionUserInfo) {
        return SnowFlakeId.nextId() + TOKEN_SPLIT + sessionUserInfo.getCorpId();
    }

    protected static String getCorpId(String token) {
        if (StringUtils.isNotBlank(token)) {
            return token.split(TOKEN_SPLIT)[1];
        }
        return null;
    }

    protected static void addCookie(HttpServletResponse servletResponse, String token) {
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
        servletResponse.addCookie(cookie);
        servletResponse.setHeader(TOKEN_HEADER_KEY, token);
        servletResponse.setHeader("Access-Control-Expose-Headers", "token");
    }

    protected static void putCache(String token, SessionUser sessionUser) {
        SessionUserCache.putCache(AuthInterceptor.SESSION_TOKEN_KEY + token, sessionUser);
    }

    @NotNull
    protected static SessionUser getSessionUserCache(CorpUserInfo sessionUserInfo, IdentitiesEnum identitiesEnum) {
        SessionUser sessionUser = new SessionUser();
        sessionUser.setCorpId(sessionUserInfo.getCorpId());
        sessionUser.setUserId(sessionUserInfo.getUserId());
        sessionUser.setUserName(sessionUser.getUserName());
        sessionUser.setIdentitiesEnum(identitiesEnum);
        return sessionUser;
    }

    @NotNull
    protected static Response<String> tokenLogout(HttpServletRequest servletRequest) {
        String token = servletRequest.getHeader(TOKEN_HEADER_KEY);
        if (StringUtils.isBlank(token)) {
            return Response.success();
        }
        SessionUser sessionUser = SessionUserCache.getCache(AuthInterceptor.SESSION_TOKEN_KEY + token);
        if (Objects.isNull(sessionUser)) {
            return Response.success();
        }
        SessionUserCache.removeCache(AuthInterceptor.SESSION_TOKEN_KEY + token);
        return Response.success();
    }


    @Nullable
    protected static String getTokenFromCookie(HttpServletRequest request) {
        if (Objects.isNull(request.getCookies())) {
            return "";
        }
        return Arrays.stream(request.getCookies()).filter(it -> Objects.equals(it.getName(), TOKEN_HEADER_KEY)).map(Cookie::getValue).findFirst().orElse(null);
    }
}
